package com.kim.bookshop.user.daoimpl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.kim.bookshop.user.bean.User;
import com.kim.bookshop.user.dao.UserDao;

import cn.itcast.jdbc.TxQueryRunner;

public class UserDaoImpl implements UserDao {
	
	private QueryRunner qr=new TxQueryRunner();
	
	@Override
	public void add(User user) {
		String sql="insert into tb_user(uid,userName,password,email,code,state,realName,address,idNo,phone) values(?,?,?,?,?,?,?,?,?,?)";
		Object[] params={user.getUid(),user.getUserName(),user.getPassword(),user.getEmail(),user.getCode(),user.getState(),user.getRealName(),user.getAddress(),user.getIdNo(),user.getPhone()};
		try {
			qr.update(sql, params);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public User findByUsername(String username) {
		String sql="select * from tb_user where userName=?";
		try {
			return qr.query(sql, new BeanHandler<User>(User.class),username);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public User findByEmail(String email) {
		String sql="select * from tb_user where email=?";
		try {
			return qr.query(sql, new BeanHandler<User>(User.class),email);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public User findByCode(String code) {
		String sql="select * from tb_user where code=?";
		try {
			return qr.query(sql, new BeanHandler<User>(User.class),code);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void updateState(String uid, int state) {
		String sql="update tb_user set state=? where uid=?";
		try {
			qr.update(sql,state,uid);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		
	}

	@Override
	public User adminLogin(String username) {
		try{
			String sql="select * from tb_user  where userName=? and role=1";
			return qr.query(sql, new BeanHandler<User>(User.class),username);
			
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<User> getList() {
		String sql="select * from tb_user";
		try {
			return qr.query(sql, new BeanListHandler<User>(User.class));
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void updatemsg(User user) {
		String sql="update tb_user set userName=?,password=?,email=? where uid=?";
		Object[] param={user.getUserName(),user.getPassword(),user.getEmail(),user.getUid()};
		try {
			qr.update(sql, param);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void kill(String uid) {
		String sql="update tb_user set state=2 where uid=?";
		try {
			qr.update(sql, uid);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public User load(String uid) {
		String sql="select * from tb_user where uid=?";
		try {
			return qr.query(sql, new BeanHandler<User>(User.class),uid);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<User> getAdmin() {
		String sql="select * from tb_user where role=1";
		try {
			return qr.query(sql, new BeanListHandler<User>(User.class));
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public void restart(String uid){
		String sql="update tb_user set state=1 where uid=?";
		try {
			qr.update(sql, uid);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void delete(String uid) {
		String sql="delete from tb_user where uid=?";
		try {
			qr.update(sql,uid);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}


}
